import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue2'
import vueJsx from '@vitejs/plugin-vue2-jsx'

import {
    createHtmlPlugin
} from 'vite-plugin-html'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import ViteCSSExportPlugin from 'vite-plugin-css-export'
import path from 'path';

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
    const env = loadEnv(mode, process.cwd())
    const port = env.port || env.npm_config_port || 1024; // 端口
    return {
        define: {
            'process.env': { ...env }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    silenceDeprecations: ['import', 'legacy-js-api', 'css-function-mixin','function-units','slash-div','global-builtin'],
                }
            },
            
            scss: {
            }
        },
        lintOnSave: false,
        plugins: [
            vue(),
            vueJsx({
                include: [/\.[jt]sx$/]
            }),
            createHtmlPlugin({
                minify: true,
                inject: {
                    data: {
                        title: env.VITE_APP_TITLE,
                        cdn_url: env.VITE_CDN_URL,
                    },
                },
            }),
            createSvgIconsPlugin({
                iconDirs: [path.join(process.cwd(), 'src/assets/icons/svg')],
                symbolId: 'icon-[dir]-[name]'
            }),

            ViteCSSExportPlugin()
        ],
        resolve: {
            /** 添加alias规则 */
            alias: [
                {
                    find: /@\/.+/,
                    replacement: (val) => {
                        return val.replace(/^@/, path.join(__dirname, './src/'))
                    },
                },
                {
                    // this is required for the SCSS modules
                    find: /^~(.*)$/,
                    replacement: '$1'
                }
            ],
            /** 暂时先加.vue, .js, .json */
            extensions: [".vue", ".js", ".json", ".scss", ".css",".jsx"],
        },
        server: {
            host: "0.0.0.0",
            port: port,
            open: true,
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    target: env.VITE_APP_BASE_API_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(new RegExp("^" + env.VITE_APP_BASE_API), '')
                },
            },
            disableHostCheck: true,
        }

    }
})
